import axios from "axios"
import {ElMessage} from "element-plus";

const defaultError = () => ElMessage.error('服务器出了点问题了喵o(TヘTo)')
const defaultFailure = (message: string) => ElMessage.warning(message)
const defaultPostConfig = {
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    },
    withCredentials: true
}
const defaultGetConfig = {
    withCredentials: true
}

export function post(url: string,
                     formData: any,
                     success: object,
                     failure = defaultFailure,
                     error = defaultError,
                     defaultConfig = defaultPostConfig,) {
    // @ts-ignore
    axios.post(url, formData, defaultConfig).then((response) => {
        // @ts-ignore
        success(response.data)
    }).catch(error)
}

export function get(url: string,
                    success: object,
                    failure = defaultFailure,
                    error = defaultError,
                    defaultConfig = defaultGetConfig,) {
    axios.get(url, defaultConfig).then(response => {
        // @ts-ignore
        success(response.data)
    }).catch(error)
}

export function post_json(url: string,
                          formData: any,
                          success: object,
                          failure = defaultFailure,
                          error = defaultError) {
    axios.post(url,formData,{
        headers: {
            'Content-Type' : 'application/json'
        },
        withCredentials: true
    })
        .then((response) => {
            if(response.data.status == 1){
                // @ts-ignore
                success(response.data)
            }else{
                failure(response.data.message)
            }
        })
        .catch(error)
}
export function put_form(url: string,
                         formData: any,
                         success: object,
                         failure = defaultFailure,
                         error = defaultError){
    axios.put(url,formData,{
        headers: {
            'Content-Type' : 'application/x-www-form-urlencoded'
        },
        withCredentials: true
    })
        .then((response) => {
            if(response.data.status == 1){
                // @ts-ignore
                success(response.data)
            }else{
                failure(response.data.message)
            }
        })
        .catch(error)
}
export function put_json(url: string,
                         formData: any,
                         success: object,
                         failure = defaultFailure,
                         error = defaultError){
    axios.put(url,formData,{
        headers: {
            'Content-Type' : 'application/json'
        },
        withCredentials: true
    })
        .then((response) => {
            if(response.data.status == 1){
                // @ts-ignore
                success(response.data)
            }else{
                failure(response.data.message)
            }
        })
        .catch(error)
}
export function deLete(url: string,
                    success: object,
                    failure = defaultFailure,
                    error = defaultError,
                    defaultConfig = defaultGetConfig,) {
    axios.delete(url, defaultConfig).then(response => {
        // @ts-ignore
        success(response.data)
    }).catch(error)
}